<?php

namespace Admin\Models\app;

use Admin\Models\BaseModel;
use Helpers\Main;
class XhappDroidwall extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iDWId;

    /**
     *
     * @var string
     */
    public $sAppName;

    /**
     *
     * @var string
     */
    public $sAppPackage;

    /**
     *
     * @var string
     */
    public $sNetwork;

    /**
     *
     * @var integer
     */
    public $bDisable;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("'XHApp_Droidwall'");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHApp_Droidwall';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhappDroidwall[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhappDroidwall
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据传入id，删除对应记录并返回删除结果
     *
     * @param $ids
     * @return mixed
     *
     * create by ck 20160923
     */
    public static function deleteRecordById($ids){
        return self::find(array(
            'conditions' => "iDWId IN ({$ids}) AND bDelete = 0"
        ))->delete();
    }

    /**
     * 根据id及数据增加或修改数据并返回
     *
     * @param array $data
     * @return XhappDroidwall
     *
     * create by ck 20160616
     * modify by ck 20160914
     * modify by ck 20160923    结构化简
     */
    public static function createRecord($data = array())
    {
        $obj = $data['iDWId'] ? self::findFirst($data['iDWId']) : new self();
        $obj or $obj = new self;
        $obj->assign($data);
        return $obj;
    }

    /**
     * 应用新增时根据应用名创建记录，已有则返回true
     *
     * @param $appName
     * @param $packageName
     * @return bool
     *
     * create by ck 20161018
     */
    public static function createRecordForNewApp($appName, $packageName)
    {
        $obj = self::findFirst("sAppName = '{$appName}'");
        if($obj) return true;
        $obj = new self;
        $obj->sAppName    = $appName;
        $obj->sAppPackage = $packageName;
        $obj->sNetwork    = '3GWIFI';
        $obj->bDisable    = 0;
        $obj->bDelete     = 0;
        return $obj->insertData();
    }

    /**
     * 根据应用名称删除防火墙记录
     *
     * @param $appNameArr
     * @return mixed
     *
     * create by ck 20161019
     */
    public static function deleteRecordByAppName($appNameArr)
    {
        //查找应用名称数组下未删除的信息（应用名称一样的情况）
        $existAppName = XhappStore::find([
            'columns'    => 'sAppName',
            'conditions' => 'bDelete = 0 AND bAppHistory = 0 AND sAppName IN ({nameArr:array})',
            'bind'       => [
                'nameArr'   => $appNameArr
            ]
        ])->toArray();
        $existAppName = array_column($existAppName, 'sAppName');

        //求差集并删除，如无差集则直接返回true
        $appNameArr = array_diff($appNameArr, $existAppName);
        if(!$appNameArr) return true;
        return self::find([
            'conditions'    => 'bDelete = 0 AND sAppName IN ({nameArr:array})',
            'bind'          => [
                'nameArr'       => $appNameArr
            ]
        ])->delete();
    }

}
